<?
        $results = array();
        require_once('../inc/Users.php');
        require_once('../inc/Keywords.php');
        if(isset($_POST['id']))
        {
				$row = array();
                $row = $_POST;
                $results = ValidateUser($row);
                if($results === true)
                        $results = SaveUser($row);
                if($results === true)
                {
					if(isset($_REQUEST['ajax'])){
                                $rs = GetUser($row['id']);
                                include('item.php');
                        }else{
                                header("Location: index.php?inserted=$row[id]#inserted");
                        }
                        die();

                }
        }else{
                if(isset($_REQUEST['id']))
				{
                        $id = intval($_REQUEST["id"]);
						$results = GetUser($id);
						$row = mysql_fetch_assoc($results);
				}
                else
                        $row = BlankUser();
        }
        $keywords = GetKeywords();//GetUserTypes();
?>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Details of user: <?=$row['first_name']?> <?=$row['last_name']?></title>
    <link rel="stylesheet" type="text/css" href="../inc/jquery-ui.css">
    <link rel="stylesheet" type="text/css" href="../inc/bootstrap.css">
    <style type="text/css" >
        .error { color: red; }
                input.error { border-color: red; }
    </style>
</head>

<body>
        <? include('../inc/header.php'); ?>
        <div class="container">
                        <? include('../inc/nav.php'); ?>
            <div id="contents">
               
                <dl class="error">
                        <? foreach ($results as $key => $value): ?>
                                                <dt><?=$key?></dt>
                                                <dd><?=$value?></dd>
                                        <? endforeach; ?>
                </dl>
               
                <form class="form-horizontal" method="post" action="edit.php">
                    <div class=" control-group">
                        <label class="control-label" for="first_name">First Name:</label>
                        <div class="controls">
                            <input      type="text" name="first_name" id="first_name" value="<?=$row['first_name']?>"
                                                                        class="required  <?=isset($results['first_name']) ? 'error' : '' ?>"
                                                         />
                             <span class="error"><?=isset($results['first_name']) ? $results['first_name'] : '' ?></span>
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label" for="last_name">Last Name:</label>
                        <div class="controls">
                            <input      type="text" name="last_name" id="last_name" value="<?=$row['last_name']?>"
                                        class="<?=isset($results['last_name']) ? 'error' : '' ?>"
                            />
                             <span class="error"><?=isset($results['last_name']) ? $results['last_name'] : '' ?></span>
                        </div>
                    </div>
                    <div class=" control-group">
                        <label class="control-label" for="phone_number">Phone Number:</label>
                        <div class="controls">
                            <input      type="text" name="phone_number" id="phone_number" value="<?=$row['phone_number']?>"
                                                                        class="required  <?=isset($results['phone_number']) ? 'error' : '' ?>"
                                                         />
                             <span class="error"><?=isset($results['phone_number']) ? $results['phone_number'] : '' ?></span>
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label" for="Key_Words_id">User Type:</label>
                        <div class="controls">
                                <select name="Key_Words_id" id="Key_Words_id" class="<?=isset($results['Key_Words_id']) ? 'error' : '' ?>">
                                <option value="">User Type</option>
                                                                <? while($krow = mysql_fetch_assoc($keywords)):  ?>
                                        <option
                                        value="<?=$krow['id']?>"
                                        <? if($krow['id'] == $row['Key_Words_id']){ ?>selected='selected'<? } ?>
                                        ><?=$krow['position']?></option>
                                <? endwhile; ?>
                            </select>
                            <span class="error"><?=isset($results['Key_Words_id']) ? $results['Key_Words_id'] : '' ?></span>
                        </div>
                    </div>
                    <div class="control-group">
                        <div class="controls">
                            <input type="hidden" name="id" value="<?=$row['id']?>" />
                                <input class="btn btn-primary" type="submit" value="Save" />
                            <a href="./" class="cancel-link">Cancel</a>
                        </div>
                    </div>
                </form>
           </div>
       </div>
       <script type="application/javascript" src='../inc/jquery-ui.js'>
           </script>
       <script type="application/javascript" src="../inc/jqueryvalidatemin.js">
           </script>
       <script type="text/javascript">
                        $(function(){
                                //$("#Key_Words_id").val('<?=$row['id']?>');
                                $("input[type='date']").datepicker();
                               
                                $("form").validate({
                                        rules: {
                                                Key_Words_id: { required: true }
                                        }
                                });
                               
                        });
           </script>
</body>
</html>
